import {
    getTrunRank,
    getOverRank,
    getLeaveRank,
    getAbnorRank,
    getLeaveType
} from '@/services/attendance/reportRank';

export default {
    namespace: 'reportRank',
    state: {
        attendanceData: [],
        leaveData: [],
        overtimeData: [],
        abnormalData: [],
        paginationAttendance: {
            current: 1,
            pageSize: 10,
            total: 0
        },
        paginationLeave: {
            current: 1,
            pageSize: 10,
            total: 0
        },
        paginationOvertime: {
            current: 1,
            pageSize: 10,
            total: 0
        },
        paginationAbnormal: {
            current: 1,
            pageSize: 10,
            total: 0
        },
        leaveType: []
    },
    effects: {
        *fetchTrunRank({ payload, callback }, { put, call }) {
            const result = yield call(getTrunRank, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        attendanceData: result.data.rows,
                        paginationAttendance: {
                            current: result.data.pageNo,
                            pageSize: result.data.pageSize,
                            total: result.data.total
                        }
                    }
                });
            }
            if (callback) callback(result);
        },
        // 加班
        *fetchOverRank({ payload, callback }, { put, call }) {
            const result = yield call(getOverRank, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        overtimeData: result.data.rows,
                        paginationOvertime: {
                            current: result.data.pageNo,
                            pageSize: result.data.pageSize,
                            total: result.data.total
                        }
                    }
                });
            }
            if (callback) callback(result);
        },
        *fetchLeaveRank({ payload, callback }, { put, call }) {
            const result = yield call(getLeaveRank, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        leaveData: result.data.rows,
                        paginationLeave: {
                            current: result.data.pageNo,
                            pageSize: result.data.pageSize,
                            total: result.data.total
                        }
                    }
                });
            }
            if (callback) callback(result);
        },
        *fetchAbnorRank({ payload, callback }, { put, call }) {
            const result = yield call(getAbnorRank, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        abnormalData: result.data.rows,
                        paginationAbnormal: {
                            current: result.data.pageNo,
                            pageSize: result.data.pageSize,
                            total: result.data.total
                        }
                    }
                });
            }
            if (callback) callback(result);
        },
        *fetchLeaveType({ payload, callback }, { put, call }) {
            const result = yield call(getLeaveType, payload);
            if (result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        leaveType: result.data
                    }
                });
                callback();
            }
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        },
        setPagination(state, { pagination }) {
            return {
                ...state,
                pagination
            };
        }
    }
};
